import tkinter as tk
from tkinter import ttk
# from Motor_test import Motor_control

# 控制函数
def control_joint(joint_id, angle):
    print(f"执行控制：关节{joint_id} -> {angle:.2f}°")
    # Motor_control(joint_id, angle)

# 滑块事件回调
def on_slider_change(joint_id, var, label_var):
    angle = var.get()
    label_var.set(f"{angle:.2f}")
    control_joint(joint_id, angle)

# 创建 GUI
root = tk.Tk()
root.title("Robot Arm Real-Time Control")
root.geometry("600x450")

for i in range(7):  # 7个关节
    #第一列
    tk.Label(root, text=f"关节{i+1}角度:").grid(row=i, column=0, padx=10, pady=5, sticky='e')

    #第二列
    var = tk.DoubleVar()
    label_var = tk.StringVar()
    slider = ttk.Scale(root, from_=-180, to=180, orient='horizontal', length=300, 
                       variable=var, command=lambda val, jid=i, v=var, lv=label_var: on_slider_change(jid+1, v, lv))
    slider.grid(row=i, column=1, padx=10, pady=5)

    #第三列
    var.set(0.0)  # 初始值设置
    label_var.set(f"{var.get():.2f}")
    tk.Label(root, textvariable=label_var, width=7).grid(row=i, column=2)

# root.mainloop()

def robot_arm_GUI():
    # root.after(1000, lambda: root.mainloop())
    root.mainloop()
